package com.hula.onion.book.server.spider.platforms.jintouxing;

import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/30 19:49
 *
 * @author liangbing
 * @version 1.0
 */
@Component
public class JinTouXingSpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {

        Map<String, Object> map = new HashMap<>(4);

        map.put("username", form.getUserName());
        map.put("password", Base64Utils.encodeToString(form.getPassword().getBytes()));
        map.put("checkCode", form.getTicket());
        map.put("source", "");

        return map;

    }

    /**
     * 解析账户余额
     * @param dc
     * @param element
     * @return
     */
    public CommonResponse parseAccountBalance(Document dc,Element element) {
        try {
            //解析数据
            AccountInfo accountInfo = new AccountInfo();

            //投资总额
            String amount = element.text();
            accountInfo.setAmount(new BigDecimal(amount));

            //累计收益
            element = dc.selectFirst("ul.income.mg-t25");
            String waitInterest = element.children().get(0).children().get(0).text();
            accountInfo.setWaitIntrest(new BigDecimal(waitInterest));

            String totalIntrest = element.children().get(2).children().get(0).text();
            accountInfo.setTotalIntrest(new BigDecimal(totalIntrest));

            return new CommonResponse(accountInfo);
        } catch (Exception e) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
    }

}
